
source('utils.R')
source('read_dataset.R')

donors = list(
  d12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component)
)

df12[,nat_quant_fine := as.numeric(as.character(nat_quant_fine))]
df16[,nat_quant_fine := as.numeric(as.character(nat_quant_fine))]
df20[,nat_quant_fine := as.numeric(as.character(nat_quant_fine))]

df12[,nat_quant:=fcase(nat_quant_fine < .5, '[0,50)',
                       nat_quant_fine < 0.9, '[50,90)',
                       nat_quant_fine < .99, '[90,99)',
                       nat_quant_fine < .999, '[99,99.9)',
                       nat_quant_fine < 1.01, '[99.9,)',
                       default = NA_character_)]

df16[,nat_quant:=fcase(nat_quant_fine < .5, '[0,50)',
                       nat_quant_fine < 0.9, '[50,90)',
                       nat_quant_fine < .99, '[90,99)',
                       nat_quant_fine < .999, '[99,99.9)',
                       nat_quant_fine < 1.01, '[99.9,)',
                       default = NA_character_)]

df20[,nat_quant:=fcase(nat_quant_fine < .5, '[0,50)',
                       nat_quant_fine < 0.9, '[50,90)',
                       nat_quant_fine < .99, '[90,99)',
                       nat_quant_fine < .999, '[99,99.9)',
                       nat_quant_fine < 1.01, '[99.9,)',
                       default = NA_character_)]

## WOMEN
d20 = df12[component %chin% donors[['d12_inds']] &
             gender %chin% c('F', 'M')]
d20[, subsq_giver := component %chin% donors[['d20_inds']]]
d20 = d20[, .(sq = mean(subsq_giver), .N), .(nat_quant, gender)]
d20[, class := 'Obama \u2192 Biden']

d12 = df12[component %chin% donors[['d12_inds']] &
             gender %chin% c('F', 'M')]
d12[, subsq_giver := component %chin% donors[['d16_inds']]]
d12 = d12[, .(sq = mean(subsq_giver), .N), .(nat_quant, gender)]
d12[, class := 'Obama \u2192 Clinton']

r12 = df12[component %chin% donors[['r12_inds']] &
             gender %chin% c('F', 'M')]
r12[, subsq_giver := component %chin% donors[['r16_inds']]]
r12 = r12[, .(sq = mean(subsq_giver), .N), .(nat_quant, gender)]
r12[, class := 'Romney \u2192 Trump \'16']

r20 = df12[component %chin% donors[['r12_inds']] &
             gender %chin% c('F', 'M')]
r20[, subsq_giver := component %chin% donors[['r20_inds']]]
r20 = r20[, .(sq = mean(subsq_giver), .N), .(nat_quant, gender)]
r20[, class := 'Romney \u2192 Trump \'20']

women = rbindlist(list(d12, d20, r12, r20))
women = women[, .(f = sq[gender == 'F'], m = sq[gender == 'M']), .(nat_quant, class)]

## NON-COL WHITE
ed = c('HS D', 'Voc', 'Less', 'Some','Bach','Grad')

d12 = df12[component %chin% donors[['d12_inds']] & ethnicity == 'W' & education %chin% ed]
d12[, subsq_giver := component %chin% donors[['d16_inds']]]
d12 = d12[, .(sq = mean(subsq_giver), .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]
d12[, class := 'Obama \u2192 Clinton']

d20 = df12[component %chin% donors[['d12_inds']] & ethnicity == 'W' & education %chin% ed]
d20[, subsq_giver := component %chin% donors[['d20_inds']]]
d20 = d20[, .(sq = mean(subsq_giver), .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]
d20[, class := 'Obama \u2192 Biden']

r12 = df12[component %chin% donors[['r12_inds']] & ethnicity == 'W' & education %chin% ed]
r12[, subsq_giver := component %chin% donors[['r16_inds']]]
r12 = r12[, .(sq = mean(subsq_giver), .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]
r12[, class := 'Romney \u2192 Trump \'16']

r20 = df12[component %chin% donors[['r12_inds']] & ethnicity == 'W' & education %chin% ed]
r20[, subsq_giver := component %chin% donors[['r20_inds']]]
r20 = r20[, .(sq = mean(subsq_giver), .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]
r20[, class := 'Romney \u2192 Trump \'20']

college = rbindlist(list(d12, r12, d20, r20))
college = college[, .(f = sq[education == F], m = sq[education == T]), .(nat_quant, class)]

## ALL ETHNICITY
eth = c('H', 'W', 'A', 'B', 'O')
d12 = df12[component %chin% donors[['d12_inds']] & ethnicity %chin% eth]
d12[, subsq_giver := component %chin% donors[['d16_inds']]]
d12 = d12[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity == 'W')]
d12[, class := 'Obama \u2192 Clinton']

d20 = df12[component %chin% donors[['d12_inds']] &
             ethnicity %chin% eth]
d20[, subsq_giver := component %chin% donors[['d20_inds']]]
d20 = d20[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity == 'W')]
d20[, class := 'Obama \u2192 Biden']

r12 = df12[component %chin% donors[['r12_inds']] & ethnicity %chin% eth]
r12[, subsq_giver := component %chin% donors[['r16_inds']]]
r12 = r12[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity == 'W')]
r12[, class := 'Romney \u2192 Trump \'16']

r20 = df12[component %chin% donors[['r12_inds']] & ethnicity %chin% eth]
r20[, subsq_giver := component %chin% donors[['r20_inds']]]
r20 = r20[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity == 'W')]
r20[, class := 'Romney \u2192 Trump \'20']

ethnicity = rbindlist(list(d12, r12, d20, r20))
ethnicity = ethnicity[, .(f = sq[ethnicity == F], m = sq[ethnicity == T]), .(nat_quant, class)]

out15 = rbindlist(list(
  'Women (Men)' = women, 'Non-College (College) White' = college, 'POC (White)' = ethnicity
  ), idcol = 'group', use.names = T)

out15$class <- factor(out15$class, levels = unique(out15$class))

## BLACK
d12 = df12[component %chin% donors[['d12_inds']] & ethnicity %chin% c('B', 'W')]
d12[, subsq_giver := component %chin% donors[['d16_inds']]]
d12 = d12[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
d12[, class := 'Obama \u2192 Clinton']

d20 = df12[component %chin% donors[['d12_inds']] & ethnicity %chin% c('B', 'W')]
d20[, subsq_giver := component %chin% donors[['d20_inds']]]
d20 = d20[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
d20[, class := 'Obama \u2192 Biden']

r12 = df12[component %chin% donors[['r12_inds']] & ethnicity %chin% c('B', 'W')]
r12[, subsq_giver := component %chin% donors[['r16_inds']]]
r12 = r12[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
r12[, class := 'Romney \u2192 Trump \'16']

r20 = df12[component %chin% donors[['r12_inds']] & ethnicity %chin% c('B', 'W')]
r20[, subsq_giver := component %chin% donors[['r20_inds']]]
r20 = r20[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
r20[, class := 'Romney \u2192 Trump \'20']

black = rbindlist(list(d12, r12, d20, r20))
black = black[, .(f = sq[ethnicity == 'B'], m = sq[ethnicity == 'W']), .(nat_quant, class)]

## ASIAN
d12 = df12[component %chin% donors[['d12_inds']] & ethnicity %chin% c('A', 'W')]
d12[, subsq_giver := component %chin% donors[['d16_inds']]]
d12 = d12[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
d12[, class := 'Obama \u2192 Clinton']

d20 = df12[component %chin% donors[['d12_inds']] & ethnicity %chin% c('A', 'W')]
d20[, subsq_giver := component %chin% donors[['d20_inds']]]
d20 = d20[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
d20[, class := 'Obama \u2192 Biden']

r12 = df12[component %chin% donors[['r12_inds']] & ethnicity %chin% c('A', 'W')]
r12[, subsq_giver := component %chin% donors[['r16_inds']]]
r12 = r12[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
r12[, class := 'Romney \u2192 Trump \'16']

r20 = df12[component %chin% donors[['r12_inds']] & ethnicity %chin% c('A', 'W')]
r20[, subsq_giver := component %chin% donors[['r20_inds']]]
r20 = r20[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
r20[, class := 'Romney \u2192 Trump \'20']

asian = rbindlist(list(d12, r12, d20, r20))
asian = asian[, .(f = sq[ethnicity == 'A'], m = sq[ethnicity == 'W']), .(nat_quant, class)]

## HISPANIC
d12 = df12[component %chin% donors[['d12_inds']] & ethnicity %chin% c('H', 'W')]
d12[, subsq_giver := component %chin% donors[['d16_inds']]]
d12 = d12[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
d12[, class := 'Obama \u2192 Clinton']

d20 = df12[component %chin% donors[['d12_inds']] & ethnicity %chin% c('H', 'W')]
d20[, subsq_giver := component %chin% donors[['d20_inds']]]
d20 = d20[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
d20[, class := 'Obama \u2192 Biden']

r12 = df12[component %chin% donors[['r12_inds']] & ethnicity %chin% c('H', 'W')]
r12[, subsq_giver := component %chin% donors[['r16_inds']]]
r12 = r12[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
r12[, class := 'Romney \u2192 Trump \'16']

r20 = df12[component %chin% donors[['r12_inds']] & ethnicity %chin% c('H', 'W')]
r20[, subsq_giver := component %chin% donors[['r20_inds']]]
r20 = r20[, .(sq = mean(subsq_giver), .N), .(nat_quant, ethnicity)]
r20[, class := 'Romney \u2192 Trump \'20']

hispanic = rbindlist(list(d12, r12, d20, r20))
hispanic = hispanic[, .(f = sq[ethnicity == 'H'], m = sq[ethnicity == 'W']), .(nat_quant, class)]

out16 = rbindlist(list(
  'Black' = black[, 1:3], 'Asian' = asian[, 1:3], 'Hispanic' = hispanic[, 1:3], 'White' = hispanic[, c(1:2, 4)]
  ), idcol = 'group',use.names = F)

out16$class <- factor(out16$class, levels = unique(out16$class))

save(out15, out16, file = 'summary_data/figA15_A16.rda')
